/**
 * Created by yangchao on 2020-4-15.
 */

const express = require("express");
const static = require('serve-static');
const bodyParer  = require('body-parser');
const app = express();
const router = express.Router();
const fs = require('fs');
const path = require('path');
const util = require('./util');

const staticDirs = ['/js', '/img', '/node_modules', '/dist', '/seeyon'];
app.all('*', function(req, res, next) {
  //代理服务器允许跨域
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With,content-type,token");
  //res.header("Access-Control-Allow-Headers", "*");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.header("Content-Type", "application/json;charset=utf-8");
  res.header("X-Powered-By",'pdf-server');
  next();
});

router.get("/", function (req, res) {
  //跳转到index.html
  //res.header("Content-Type", "text/html; charset=utf-8");
  return res.redirect(302, '/test/index.html');

});

router.get("/test/index.html", function (req, res) {
  //返回index.html
  res.header("Content-Type", "text/html; charset=utf-8");
  const url = path.join(__dirname,'../test/index.html');
  return res.sendFile(url);

});

router.get("/test/form.html", function (req, res) {
  //返回form.html
  res.header("Content-Type", "text/html; charset=utf-8");
  const url = path.join(__dirname,'../test/form.html');
  return res.sendFile(url);

});

router.get("/test/temp.html", function (req, res) {
  //PDF临时页面
  res.header("Content-Type", "text/html; charset=utf-8");
  const url = path.join(__dirname,'../test/temp.html');
  return res.sendFile(url);

});

router.get("/test/form/formData", function (req, res) {
  res.header("Content-Type", "application/json; charset=utf-8");
  const data = {
    code : 0,
    data: [],
    title: '这是后台返回的打印页面的title'
  };

  const random = Math.random();
  const delay =  random > 0.7 ? 32000 :
    random > 0.4 ? 5000 : 0;
  console.log(util.getCurTimestamp() + ' delay: ' + delay);
  //模拟后台请求延迟
  setTimeout(function () {
    res.status(200).send(data);
  }, 0)

});

router.get("/seeyon/*", function (req, res) {
  var filename = req.url.split('seeyon')[req.url.split('seeyon').length-1];
  var url = path.join(__dirname, '../seeyon', filename.split('?')[0]);
  var delay = 0;
  if (filename.indexOf('.html') !== -1) {
    res.header("Content-Type", "text/html; charset=utf-8");
  } else if (filename.indexOf('.js') !== -1) {
    res.header("Content-Type", "application/javascript; charset=utf-8");
  } else if (filename.indexOf('.css') !== -1) {
    res.header("Content-Type", "text/css; charset=utf-8");
    res.header("Cache-Control", "max-age:315360000");
  } else if (filename.indexOf('.png') !== -1 || filename.indexOf('.jpg') !== -1) {
    delay = 5000;
  }

  setTimeout(function () {
    return res.sendFile(url);
  }, delay);

});

app.use(bodyParer.json({limit: '800mb'}));
app.use(bodyParer.urlencoded({limit: '800mb', extended: true}));
app.use('/', router);
staticDirs.forEach(function(staticDir){
  app.use(staticDir, static(__dirname + staticDir));
});


const server = app.listen(9000, function () {
  console.log(new Date().format('yyyy-MM-dd hh:mm.ss') + ' \033[42;30m Http Server Started \033[0m');
});